package com.raising.modules.sys.controller;

import com.google.common.collect.Maps;
import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.modules.sys.entity.Dict;
import com.raising.modules.sys.entity.DictItem;
import com.raising.modules.sys.service.DictItemService;
import com.raising.modules.sys.service.DictService;
import com.raising.modules.sys.utils.DictUtils;
import com.raising.utils.JCacheUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 字典表 控制器
 * @author gaoy
 * @version 1.0
 */
@Controller
@RequestMapping("${adminPath}/sys/dict")
public class DictController extends BaseController {

    @Autowired
    private DictService dictService;
    @Autowired
    private DictItemService dictItemService;

    /**
     * 跳转字典表列表
     * @author GaoYuan
     * @datetime 2017/12/6 上午9:45
     */
    @RequiresPermissions("sys:dict:view")
    @RequestMapping(value = "/list")
    public String list() throws Exception{
        olog("进入字典表列表页面");
        return "sys/dict/list";
    }

    /**
     * 查询字典表列表数据【json】
     * @author GaoYuan
     * @datetime 2017/12/6 上午9:45
     */
    @RequiresPermissions("sys:dict:view")
    @RequestMapping(value = "/list.json",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResultVo listJson(Dict dict, Page<Dict> page) throws Exception{
        olog("查询字典表列表数据");
        page.setEntity(dict);
        page.setOrderBy(" value asc ");
        return ResultVo.success().data(dictService.getPage(page));
    }
    /**
     * 查询字典表明细数据
     * @author GaoYuan
     * @datetime 2017/12/6 上午9:45
     */
    @RequiresPermissions("sys:dict:view")
    @RequestMapping(value = "/listItems.json",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public ResultVo listItemsJson(DictItem dictItem) throws Exception{
        return ResultVo.success().data(dictItemService.getList(dictItem));
    }
    /**
     * 跳转字典表详情页面
     * @author GaoYuan
     * @datetime 2017/12/6 上午9:45
     */
    @RequiresPermissions("sys:dict:view")
    @RequestMapping(value = "/view")
    public String view(Model model, String id) throws Exception{
        olog("进入字典表详情页面","{id:"+id+"}");
        model.addAttribute("dict", dictService.get(id));
        DictItem item = new DictItem();
        item.setDictId(id);
        List<DictItem> dictItemList = dictItemService.getList(item);
        model.addAttribute("dictItemList", dictItemList);
        return "sys/dict/view";
    }
    /**
     * 跳转创建页面
     * @author GaoYuan
     * @datetime 2017/12/6 上午9:46
     */
    @RequiresPermissions("sys:dict:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Model model) {
        olog("进入字典表创建页面");
        model.addAttribute("dict", new Dict());
        return "sys/dict/form";
    }
    /**
     * 新增数据
     * @author GaoYuan
     * @datetime 2017/12/6 上午9:46
     */
    @RequiresPermissions("sys:dict:create")
    @RequestMapping(value = "/create.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo create(Dict dict, RedirectAttributes redirectAttributes) {
        olog("新增字典表["+dict.getLabel()+"]");
        dictService.saveDictAndItems(dict);
        JCacheUtils.remove(DictUtils.CACHE_DICT_MAP);
        return ResultVo.success().desc("新增成功");
    }
    /**
     * 删除字典表明细数据项
     * @author GaoYuan
     * @datetime 2017/12/6 上午9:46
     */
    @RequiresPermissions("sys:dict:delete")
    @RequestMapping(value = "/deleteItem.json", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo deleteItem(String id) throws Exception{
        olog("删除字典表明细","{id:"+id+"}");
        ResultVo resultVo = dictItemService.delete(id);
        JCacheUtils.remove(DictUtils.CACHE_DICT_MAP);
        return resultVo;
    }
    /**
     * 跳转到更新页面
     * @author GaoYuan
     * @datetime 2017/12/6 上午9:46
     */
    @RequiresPermissions("sys:dict:update")
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(String id, Model model)throws Exception {
        olog("进入字典表更新页面");
        model.addAttribute("dict", dictService.get(id));
        DictItem item = new DictItem();
        item.setDictId(id);
        model.addAttribute("dictItemList", dictItemService.getList(item));
        return "sys/dict/form";
    }
    /**
     * 执行更新
     * @author GaoYuan
     * @datetime 2017/12/6 上午9:46
     */
    @RequiresPermissions("sys:dict:update")
    @RequestMapping(value = "/update.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo update(Dict dict, RedirectAttributes redirectAttributes) {
        olog("更新字典表["+dict.getLabel()+"]数据");
        dictService.saveDictAndItems(dict);
        JCacheUtils.remove(DictUtils.CACHE_DICT_MAP);
        return ResultVo.success().desc("修改成功");
    }

    /**
     * 执行删除字典数据
     * @author GaoYuan
     * @param id 删除的对象标识
     * @datetime 2017/12/6 上午9:41
     */
    @RequiresPermissions("sys:dict:delete")
    @RequestMapping(value = "/delete.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo delete(String id) {
        olog("删除字典表数据","{id:"+id+"}");
        JCacheUtils.remove(DictUtils.CACHE_DICT_MAP);
        return dictService.delete(id);
    }

    /**
     * 刷新缓存(为了线程安全，删除的字典KEY，并不会被移除)
     * @author gaoyuan
     * @createTime 2018-12-28 09:39:11
     */
    @RequiresPermissions("sys:dict:delete")
    @RequestMapping(value = "/refreshCache.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo refreshCacheJson() throws Exception{
        Map<String, List<DictItem>> dictMap = Maps.newHashMap();
        for (DictItem dictItem : dictItemService.getList(null)) {
            List<DictItem> dictItemList = dictMap.get(dictItem.getDictValue());
            if(dictItemList == null){
                dictItemList = new ArrayList<>();
            }
            dictItemList.add(dictItem);
            dictMap.put(dictItem.getDictValue(), dictItemList);
        }
        JCacheUtils.put(DictUtils.CACHE_DICT_MAP, dictMap);
        return new ResultVo(ResultVo.SUCCESS, ResultVo.CODE_OK, "刷新成功", "");
    }

}
